package com.zw.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import com.zw.dto.RegisterDto;
import com.zw.dto.SaveUserDto;
import com.zw.dto.UserListDto;
import com.zw.entity.SysUser;
import com.zw.service.SysUserService;
import com.zw.utils.Md5Utils;
import com.zw.utils.SecurityUtils;
import com.zw.vo.LoginUser;
import com.zw.vo.ResponseVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * 用户信息表(SysUser)表控制层
 *
 * @author makejava
 * @since 2025-01-02 17:02:48
 */
@RestController
@RequestMapping("sysUser")
@Api(value = "用户信息API", tags = "用户信息API")
public class SysUserController {
    /**
     * 服务对象
     */
    @Resource
    private SysUserService sysUserService;



    /**
     * 用户注册
     *
     * @param registerDto 注册信息
     * @return 结果
     */
    @ApiOperation("用户注册")
    @PostMapping("/register")
    public ResponseVO<Boolean> register(@RequestBody @Valid RegisterDto registerDto) {
        try {
            Boolean register = sysUserService.register(registerDto);
            return new ResponseVO(register);
        } catch (Exception e) {
            e.printStackTrace();
            if (e instanceof RuntimeException){
                throw new RuntimeException(e.getMessage());
            }
            throw new RuntimeException("用户注册失败，请联系管理员处理！");
        }
    }
    /**
     * 查询用户列表
     *
     * @return 全部数据
     */
    @GetMapping("/list")
    public ResponseEntity<PageInfo<SysUser>> queryAllUsers(@RequestBody @Valid UserListDto dto) {
        return sysUserService.queryAllUsers(dto);
    }

    /**
     * 新增、编辑用户
     *
     * @return 单条数据
     */
    @GetMapping("/saveOrUpdateUser")
    public ResponseEntity<Boolean> saveOrUpdateUser(@RequestBody @Valid SaveUserDto dto) {
        if( !"admin".equals(SecurityUtils.getRole())){
            throw new RuntimeException("您暂无权限操作此项，请联系超管人员！");
        }
        Boolean result = this.sysUserService.saveOrUpdateUser(dto);
        return ResponseEntity.ok(result);
    }
    /**
     * 删除用户
     *
     * @return 单条数据
     */
    @GetMapping("/deleteUserById")
    public ResponseEntity<Boolean> deleteUserById(@RequestParam(value = "userId") String userId) {
       if( !"admin".equals(SecurityUtils.getRole())){
            throw new RuntimeException("您暂无权限操作此项，请联系超管人员！");
       }
        Boolean result = this.sysUserService.removeById(userId);
        return ResponseEntity.ok(result);
    }
    /**
     * 用户状态修改
     *
     * @return 单条数据
     */
    @GetMapping("/updateUserStatusById")
    public ResponseEntity<Boolean> updateUserStatusById(@RequestParam(value = "userId") String userId,
                                                        @RequestParam(value = "userStatus") String userStatus) {
        if( !"admin".equals(SecurityUtils.getRole())){
            throw new RuntimeException("您暂无权限操作此项，请联系超管人员！");
        }
        SysUser sysUser = new SysUser();
        sysUser.setStatus(userStatus);
        Boolean result = this.sysUserService.update(sysUser,new QueryWrapper<SysUser>().eq("user_id",userId));
        return ResponseEntity.ok(result);
    }
    /**
     * 重置密码
     *
     * @return 单条数据
     */
    @GetMapping("/resetUserPassword")
    public ResponseEntity<Boolean> resetUserPassword(@RequestParam(value = "userId") String userId) {
        if( !"admin".equals(SecurityUtils.getRole())){
            throw new RuntimeException("您暂无权限操作此项，请联系超管人员！");
        }
        SysUser user = sysUserService.getById(userId);
        SysUser sysUser = new SysUser();
        //重置密码为"123456"
        sysUser.setPassword(Md5Utils.strMd5(Md5Utils.strMd5("123456") + user.getMaskNum()));
        Boolean result = this.sysUserService.update(sysUser,new QueryWrapper<SysUser>().eq("user_id",userId));
        return ResponseEntity.ok(result);
    }
}

